package com.yy.controller;

import org.apache.commons.io.FileUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

/**
 * @Author: yy
 * @Date 2021/12/1 20:40
 */
@CrossOrigin
@RestController
@RequestMapping("/ensure")
public class MyController {
    //上传文件
    @PostMapping("/uploadwork")
    public String uploadWork(HttpServletRequest request, @RequestParam(value = "file", required = false) MultipartFile file) throws IOException {

        request.setCharacterEncoding("UTF-8");
        String user = request.getParameter("user");

        if(!file.isEmpty()) {
            String fileName = file.getOriginalFilename();
            String path = null;
            String type = fileName.indexOf(".") != -1 ? fileName.substring(fileName.lastIndexOf(".") + 1, fileName.length()) : null;
            if (type != null) {
                if ("DOCX".equals(type.toUpperCase())||"DOC".equals(type.toUpperCase())) {
                    // 项目在容器中实际发布运行的根路径
                    String realPath = request.getSession().getServletContext().getRealPath("/");
                    // 自定义的文件名称
                    String trueFileName = user + "_" + fileName;

                    // 设置存放图片文件的路径
                    //path = "/static/File" + trueFileName;
                    path = "D:\\文件" + new SimpleDateFormat("yyyyMMddHHmmss").format(new Date()) + "_" + fileName;
                    File dest = new File(path);
                    //判断文件父目录是否存在
                    if (!dest.getParentFile().exists()) {
                        dest.getParentFile().mkdir();
                    }

                    file.transferTo(dest);

                    return trueFileName;
                }else {
                    return "error1";
                }
            }else {
                return "error2";
            }
        }else {
            return "error3";
        }
    }

    SimpleDateFormat sdf =new SimpleDateFormat("/yyyy/MM/dd/");

    @PostMapping("/upload")
    public Map<String,Object> fileload(MultipartFile file,HttpServletRequest request){
        Map<String,Object> result = new HashMap<>();
        String originName = file.getOriginalFilename();
        if (!originName.endsWith(".pdf")){
            result.put("status", "error1");
            result.put("msg", "文件类型错误");
            return result;
        }
        String format = sdf.format(new Date());
        String realPath = request.getServletContext().getRealPath("/") + format;

        String filepath="D:\\idea project\\newapp\\src\\main\\resources\\static\\File" +format;
        File folder = new File(filepath);

        if (!folder.exists()){
            folder.mkdir();
        }
        String newName = UUID.randomUUID().toString() + ".pdf";
        try {
            //file.transferTo(new File(folder,newName));
            //file.transferTo(folder.getAbsoluteFile());
            String filepath1=filepath+newName;
            File folder1 = new  File(filepath1);
            FileUtils.copyInputStreamToFile(file.getInputStream(),folder1);

            String url = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + format  +newName;
            result.put("status", "success");
            result.put("url",url);
            result.put("realPath",realPath);
            result.put("folder",folder);
            result.put("newName",newName);

        } catch (IOException e) {
            result.put("status", "error2");
            result.put("msg", e.getMessage());
        }
        return result;
    }
}

